﻿package mortal.game.view.warehouse
{
    import Message.Public.*;
    import com.mui.controls.*;
    import com.mui.events.*;
    import com.mui.utils.*;
    import extend.language.*;
    import fl.controls.*;
    import fl.data.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.cache.packCache.*;
    import mortal.game.events.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.item.*;
    import mortal.game.view.pack.*;
    import mortal.game.view.warehouse.panel.*;
    import mortal.game.view.wizard.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class WarehouseModule extends BaseWindow
    {
        private var _packTabData:Array;
        private var _packPageData:Array;
        private var _pageSize:int = 49;
        private var _posType:int = 0;
        private var _tabBar:GTabarNew;
        private var _packItemPanel:GTileList;
        private var _bottomPart:WarehouseBottomPart;
        private var _dragable:Boolean = true;
        private var _dropable:Boolean = true;

        public function WarehouseModule(param1:ILayer = null)
        {
            this._packTabData = Language.getArray(30057);
            this._packPageData = [{name:"page1", label:"1", pageIndex:0}, {name:"page2", label:"2", pageIndex:1}, {name:"page3", label:"3", pageIndex:2}, {name:"page4", label:"4", pageIndex:3}];
            this.init();
            return;
        }// end function

        private function init() : void
        {
            setSize(345, 500);
            title = Language.getString(35607);
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this.mouseEnabled = true;
            this.pushUIToDisposeVec(UIFactory.bg(8, 62, 333, 328, this, ImagesConst.PanelBg));
            this._tabBar = UIFactory.gTabBarNew(2, 32, this._packTabData, 343, 500, 65, 26, this, this.tabBarChangeHandler, "TabButtonNew");
            this._tabBar.selectedIndex = 0;
            this._packItemPanel = UIFactory.tileList(10, 65, 344, 322, this);
            this._packItemPanel.rowHeight = 46;
            this._packItemPanel.columnWidth = 46;
            this._packItemPanel.horizontalGap = -1;
            this._packItemPanel.verticalGap = 0;
            this._packItemPanel.horizontalScrollPolicy = ScrollPolicy.ON;
            this._packItemPanel.verticalScrollPolicy = ScrollPolicy.ON;
            this._packItemPanel.setStyle("cellRenderer", WarehouseCellRenderer);
            this._packItemPanel.configEventListener(DragEvent.Event_Move_In, this.moveInBagHandler);
            this._packItemPanel.isClickedUpdate = false;
            this._bottomPart = UICompomentPool.getUICompoment(WarehouseBottomPart);
            this._bottomPart.createDisposedChildren();
            this._bottomPart.x = 9;
            this._bottomPart.y = 410;
            this.addChild(this._bottomPart);
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl(param1);
            this._tabBar.dispose(param1);
            this._bottomPart.dispose(param1);
            this._packItemPanel.dispose(param1);
            this._tabBar = null;
            this._bottomPart = null;
            this._packItemPanel = null;
            return;
        }// end function

        private function tabBarChangeHandler(event:MuiEvent = null) : void
        {
            this.updateAllItems();
            return;
        }// end function

        public function updateAllItems() : void
        {
            if (this._packItemPanel)
            {
                this._packItemPanel.dataProvider = this.getDataProvider();
                this._packItemPanel.drawNow();
            }
            this.updateCapacity();
            return;
        }// end function

        private function getDataProvider() : DataProvider
        {
            var _loc_2:Array = null;
            var _loc_3:int = 0;
            var _loc_5:int = 0;
            var _loc_6:Object = null;
            var _loc_1:* = new DataProvider();
            var _loc_4:* = Cache.instance.pack.warehouseCache;
            if (_loc_4 && _loc_4.sbag)
            {
                switch(this._tabBar.selectedIndex)
                {
                    case 0:
                    {
                        this._pageSize = Math.ceil(Cache.instance.pack.warehouseCache.currentGrid / 7) * 7 < 49 ? (49) : (Math.ceil(Cache.instance.pack.warehouseCache.currentGrid / 7) * 7);
                        _loc_2 = Cache.instance.pack.warehouseCache.getItemsAtPage(1, 35);
                        break;
                    }
                    case 1:
                    {
                        this._pageSize = Cache.instance.pack.warehouseCache.currentGrid < 49 ? (49) : (var _loc_7:* = Cache.instance.pack.warehouseCache.currentGrid, this._pageSize = Cache.instance.pack.warehouseCache.currentGrid, _loc_7);
                        _loc_2 = Cache.instance.pack.warehouseCache.getEquipItemsAtPage(1, 35);
                        break;
                    }
                    case 2:
                    {
                        this._pageSize = Cache.instance.pack.warehouseCache.currentGrid < 49 ? (49) : (var _loc_7:* = Cache.instance.pack.warehouseCache.currentGrid, this._pageSize = Cache.instance.pack.warehouseCache.currentGrid, _loc_7);
                        _loc_2 = Cache.instance.pack.warehouseCache.getPropItemsAtPage(1, 35);
                        break;
                    }
                    case 3:
                    {
                        this._pageSize = Cache.instance.pack.warehouseCache.currentGrid < 49 ? (49) : (var _loc_7:* = Cache.instance.pack.warehouseCache.currentGrid, this._pageSize = Cache.instance.pack.warehouseCache.currentGrid, _loc_7);
                        _loc_2 = Cache.instance.pack.warehouseCache.getStuffsItemsAtPage(1, 35);
                        break;
                    }
                    default:
                    {
                        break;
                    }
                }
            }
            if (_loc_2)
            {
                _loc_3 = _loc_2.length;
                _loc_5 = 0;
                while (_loc_5 < _loc_3)
                {
                    
                    _loc_6 = new Object();
                    _loc_6.item = _loc_2[_loc_5];
                    _loc_1.addItem(_loc_6);
                    _loc_5++;
                }
            }
            return _loc_1;
        }// end function

        private function moveInBagHandler(event:DragEvent) : void
        {
            var _loc_2:BaseItem = null;
            var _loc_3:BaseItem = null;
            var _loc_4:ItemData = null;
            var _loc_5:ItemData = null;
            var _loc_6:DragDropData = null;
            if (this._dropable)
            {
                _loc_2 = event.dragItem as BaseItem;
                _loc_3 = event.dropItem as BaseItem;
                _loc_4 = event.dragSouce as ItemData;
                _loc_5 = _loc_3.itemData;
                if (_loc_2 && _loc_3 && _loc_2 == _loc_3 || _loc_2 == null || this._tabBar.selectedIndex != 0)
                {
                    return;
                }
                if (_loc_4 && (this._posType == EPlayerItemPosType._EPlayerItemPosTypeBag || this._posType == _loc_4.posType))
                {
                    _loc_6 = new DragDropData(_loc_4.serverData.posType, this._posType, _loc_4.serverData.uid, _loc_2.pos, _loc_3.pos, _loc_4, _loc_5);
                    Dispatcher.dispatchEvent(new DataEvent(EventName.Warehouse_DragInItem, _loc_6));
                }
            }
            event.stopImmediatePropagation();
            return;
        }// end function

        public function updateCapacity() : void
        {
            if (this._bottomPart)
            {
                this._bottomPart.updateCapacity();
            }
            return;
        }// end function

        public function showUnLockItem(param1:Object) : void
        {
            var _loc_2:* = Cache.instance.pack.warehouseCache;
            var _loc_3:* = new Array();
            var _loc_4:* = int(param1);
            var _loc_5:* = _loc_2.currentGrid - 1;
            while (_loc_5 < _loc_4)
            {
                
                _loc_3.push(_loc_5);
                _loc_5++;
            }
            this._packItemPanel.selectedIndices = _loc_3;
            return;
        }// end function

        public function hideUnLockItem() : void
        {
            if (this._packItemPanel)
            {
                this._packItemPanel.selectedIndices = null;
            }
            return;
        }// end function

    }
}
